//
//  PreviousViewController.m
//  UIViewControllerTransitionDemo
//
//  Created by Guo Rui on 2017/7/3.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "PreviousViewController.h"
#import "PreviousRedController.h"
#import "PreviousBlueController.h"

@interface PreviousViewController ()
@property (strong, nonatomic) PreviousRedController *redViewController;
@property (strong, nonatomic) PreviousBlueController *blueViewController;
@property (assign, nonatomic) BOOL isRed;
@end

@implementation PreviousViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem =
    [[UIBarButtonItem alloc] initWithTitle:@"transition" style:UIBarButtonItemStylePlain target:self action:@selector(transition)];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [self showRedViewController:NO];
}

- (void)transition {
    if (self.isRed) {
        [self showBlueViewController];
    }
    else {
        [self showRedViewController:YES];
    }
}

- (void)showBlueViewController {
    self.isRed = NO;
    
    PreviousBlueController *blueViewController = [[PreviousBlueController alloc] init];
    
    [self addChildViewController:blueViewController];
    [blueViewController willMoveToParentViewController:self];
    [self.view addSubview:blueViewController.view];
    
    __weak typeof(self) weakself = self;
    [self transitionFromViewController:self.redViewController
                      toViewController:blueViewController
                              duration:.3f
                               options:UIViewAnimationOptionTransitionFlipFromTop
                            animations:nil
                            completion:^(BOOL finished) {
                                [weakself.redViewController.view removeFromSuperview];
                                [weakself.redViewController removeFromParentViewController];
                                [blueViewController didMoveToParentViewController:weakself];
                            }];
    
    self.blueViewController = blueViewController;
}

- (void)showRedViewController:(BOOL)animated {
    self.isRed = YES;
    
    PreviousRedController *redViewController = [[PreviousRedController alloc] init];
    
    [self addChildViewController:redViewController];
    [redViewController willMoveToParentViewController:self];
    [self.view addSubview:redViewController.view];
    
    if (animated) {
        __weak typeof(self) weakself = self;
        [self transitionFromViewController:self.blueViewController
                          toViewController:redViewController
                                  duration:.3f
                                   options:UIViewAnimationOptionTransitionFlipFromTop
                                animations:^{
                                    
                                } completion:^(BOOL finished) {
                                    [weakself.blueViewController.view removeFromSuperview];
                                    [weakself.blueViewController removeFromParentViewController];
                                    [redViewController didMoveToParentViewController:weakself];
                                }];
    }
    else {
        [redViewController didMoveToParentViewController:self];
    }
    
    self.redViewController = redViewController;
}

@end
